
package com.itheima;

import com.itheima.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    public static void main(String[] args) {
        // 创建集合用于存储学生对象
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        while (true) {
            // 输出主界面

            System.out.println("-----欢迎使用学生管理系统-----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.print("请输入您的选择：");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    addStudent(list, sc);
                    break;
                case "2":
                    deleteStudent(list, sc);
                    break;
                case "3":
                    updateStudent(list, sc);
                    break;
                case "4":
                    showStudent(list);
                    break;
                case "5":
                    System.out.println("感谢使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
    }

    // 添加学生方法
    public static void addStudent(ArrayList<Student> list, Scanner sc) {
        while (true) {
            System.out.print("请输入学生学号：");
            String id = sc.next();
            int index = getIndex(list, id);
            if (index == -1) {
                System.out.print("请输入学生姓名：");
                String name = sc.next();
                System.out.print("请输入学生年龄：");
                int age = sc.nextInt();
                System.out.print("请输入学生住址：");
                String address = sc.next();

                Student student = new Student(id, name, age, address);
                list.add(student);
                System.out.println("添加成功！");
                break;
            } else {


                比奇堡专爱干饭无名小鱼:
                System.out.println("学号已存在，请重新输入！");
            }
        }
    }

    // 查看学生方法
    public static void showStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无学生信息，请先添加！");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t住址");
        for (Student student : list) {
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "岁\t\t" + student.getAddress());
        }
    }

    // 删除学生方法
    public static void deleteStudent(ArrayList<Student> list, Scanner sc) {
        System.out.print("请输入要删除的学生学号：");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("该学号学生信息不存在！");
        } else {
            list.remove(index);
            System.out.println("删除成功！");
        }
    }

    // 修改学生方法
    public static void updateStudent(ArrayList<Student> list, Scanner sc) {
        System.out.print("请输入要修改的学生学号：");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("该学号学生信息不存在！");
        } else {
            System.out.print("请输入修改后的学生姓名：");
            String name = sc.next();
            System.out.print("请输入修改后的学生年龄：");
            int age = sc.nextInt();
            System.out.print("请输入修改后的学生住址：");
            String address = sc.next();

            Student student = list.get(index);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);
            System.out.println("修改成功！");
        }
    }

    // 获取学生在集合中的索引方法
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}

